﻿using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using System.Linq.Expressions;
using Apps.IDAL.RC;
using Apps.IBLL.RC;
using Apps.IBLL;
using Apps.Models.MIS;

namespace Apps.BLL.RC
{
    public partial class RC_TRAINNUMBLL : IRC_TRAINNUMBLL
    {

        [Dependency]
        public IRC_TRAINNUMRepository m_Rep { get; set; }

        public DBContainer db = new DBContainer();

        public virtual List<V_RC_TRAINNUM_UNLOADINFO> GetList(ref GridPager pager, string queryStr, string sDate, string eDate)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    var whereLambda = PredicateBuilder.True<V_RC_TRAINNUM_UNLOADINFO>();
                    if (!string.IsNullOrEmpty(queryStr))
                    {
                        whereLambda = whereLambda.And(p => p.TRAIN_NUM.Contains(queryStr));
                    }
                    if (string.IsNullOrWhiteSpace(sDate))
                    {
                        sDate = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                    }
                    if (string.IsNullOrWhiteSpace(eDate))
                    {
                        eDate = DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd");
                    }
                    DateTime dt1 = Convert.ToDateTime(sDate + " 00:00:00");
                    DateTime dt2 = Convert.ToDateTime(eDate + " 23:59:59");

                    whereLambda = whereLambda.And(p => DateTime.Compare((DateTime)p.GROSS_TIME, dt1) >= 0 && DateTime.Compare((DateTime)p.GROSS_TIME, dt2) <= 0);
                    var queryData = db.V_RC_TRAINNUM_UNLOADINFO.Where(whereLambda.Compile()).AsQueryable();

                    pager.totalRows = queryData.Count();
                    //排序
                    queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                    return queryData.ToList();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public virtual List<RC_TRAINNUMModel> GetListByParentId(ref GridPager pager, object parentId)
        {
            using (DBContainer db = new DBContainer())
            {
                string parid = parentId.ToString();
                var whereLambda = PredicateBuilder.True<RC_TRAINNUM>();
                if (!string.IsNullOrEmpty(parid))
                {
                    whereLambda = whereLambda.And(p => p.TRAIN_NUM == parid);
                }
                IQueryable<RC_TRAINNUM> queryDataBase = db.RC_TRAINNUM.Where(whereLambda.Compile()).AsQueryable();

                var whereLambdaPlan = PredicateBuilder.True<PL_HC_PLANS>();
                var sDate = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");
                var eDate = DateTime.Now.AddMonths(1).ToString("yyyy-MM-dd");
                DateTime dt1 = Convert.ToDateTime(sDate + " 00:00:00");
                DateTime dt2 = Convert.ToDateTime(eDate + " 23:59:59");
                whereLambdaPlan = whereLambdaPlan.And(p => p.PUBLISHED == true && DateTime.Compare((DateTime)p.PUBLISH_TIME, dt1) >= 0 && DateTime.Compare((DateTime)p.PUBLISH_TIME, dt2) <= 0);
                IQueryable<PL_HC_PLANS> queryDataPlan = db.PL_HC_PLANS.Where(whereLambdaPlan.Compile()).AsQueryable();

                var queryData = (from t0 in queryDataBase
                                 join t1 in queryDataPlan
                                 on t0.PLAN_ID equals t1.PLAN_ID 
                                 select new RC_TRAINNUMModel
                                 {
                                     TRAINNUM_ID = t0.TRAINNUM_ID,
                                     PLAN_ID = t0.PLAN_ID,
                                     BATCH_CODE = t1.BATCH_CODE,
                                     LAST_READ_TIME = t0.LAST_READ_TIME,
                                     DEV_NODE = t0.DEV_NODE + "#翻车机",
                                     CAR_NUM = t0.CAR_NUM,
                                     CAR_TYPE = t0.CAR_TYPE,
                                     SERIAL_NUM = t0.SERIAL_NUM,
                                     TRAIN_NUM = t0.TRAIN_NUM,
                                     STATE = t0.STATE
                                 });
                pager.totalRows = queryData.Count();
                //排序
                queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
                return queryData.ToList();
            }
        }

        public bool Edit(ref ValidationErrors errors, RC_TRAINNUMModel model)
        {
            try
            {
                RC_TRAINNUM entity = m_Rep.GetById(model.TRAINNUM_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                entity.PLAN_ID = model.PLAN_ID;
                entity.STATE = model.STATE;
                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public bool Edit(ref ValidationErrors errors, RC_TRAINNUM model)
        {
            throw new NotImplementedException();
        }

        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
        public void Dispose()
        {

        }

        public List<RC_TRAINNUM> GetList(ref GridPager pager, string queryStr)
        {
            throw new NotImplementedException();
        }

        public bool Create(ref ValidationErrors errors, RC_TRAINNUM model)
        {
            throw new NotImplementedException();
        }

        public bool Delete(ref ValidationErrors errors, object id)
        {
            throw new NotImplementedException();
        }

        public bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            throw new NotImplementedException();
        }

        public RC_TRAINNUM GetById(object id)
        {
            throw new NotImplementedException();
        }

        public void SaveImportData(IEnumerable<RC_TRAINNUM> personList)
        {
            throw new NotImplementedException();
        }

        public bool CheckImportData(string fileName, List<RC_TRAINNUM> personList, ref ValidationErrors errors)
        {
            throw new NotImplementedException();
        }

        List<RC_TRAINNUM> IBaseBLL<RC_TRAINNUM>.GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            throw new NotImplementedException();
        }


    }
}
